<!DOCTYPE html>
<html>
<head>
	<title>test</title>
</head>
<body>
	<script type="text/javascript">

    const GenerateIncId = (function(){
      let _incIndex = 0;
      class innerGenerateIncId {

        constructor(incStep = 1){
          this.incStep = incStep;
        }

        next () {
          return _incIndex+=this.incStep;
        }
      }
      return innerGenerateIncId;
    })();


    let generate1 = new GenerateIncId(2);
    var a = generate1.next();
    var b = generate1.next();
    console.error(a);
    console.error(b);

    let generate2 = new GenerateIncId(2);
    var c = generate2.next();
    console.error(c)

    class QuarterData {
      constructor(saleItems = [], digits = 2){
        // 原始数据
        this.saleItems = saleItems;

        // 季度相关数据
        this.sumOfQuarterItems = [];

        //金额保留的小数位数
        this.digits = Math.pow(10, digits);

        this.quarterData();

        return this;
      }

      quarterData () {
        let res = [];
        this.saleItems.forEach((item) => {
          let quarterId = this.getQuarter(item.month).quarter;

        if (!res[quarterId]) {
          res[quarterId] = {
            quarter: quarterId,
            totalPrices: 0,
            transactionNums: 0
          };
        }

        res[quarterId].totalPrices = (res[quarterId].totalPrices*this.digits + item.salePrice*this.digits)/this.digits;
        res[quarterId].transactionNums += 1;
      });

        this.sumOfQuarterItems = res.filter((item) => {
          // 增加平均值
          item ? (item.averagePrices = (item.totalPrices*this.digits / item.transactionNums) / this.digits) : '';
        return item !== undefined;
      });

        return this;
      }

      // quarter
      getQuarter (month) {
        let spring = 0; //春
        let summer = 3; //夏
        let fall = 6;   //秋
        let winter = 9; //冬

        if (month < summer) {
          return {
            key: 'spring',
            quarter: 1
          };
        }

        if (month < fall) {
          return {
            key: 'summer',
            quarter: 2
          };
        }


        if (month < winter) {
          return {
            key: 'fall',
            quarter: 3
          };
        }


        return {
          key: 'winter',
          quarter: 3
        };
      }
    }


    function sumByQuarter(saleItems) {
      let sum = new QuarterData(saleItems).sumOfQuarterItems;
      let res = [];

      sum.forEach((item) => {
        res.push({
        quarter: item.quarter,
        totalPrices: item.totalPrices,
        transactionNums: item.transactionNums
      });
    });
      return res;
    }

    function averageByQuarter(saleItems) {
      let average = new QuarterData(saleItems).sumOfQuarterItems;
      let res = [];

      average.forEach((item) => {
        res.push({
        quarter: item.quarter,
        averagePrices: item.averagePrices,
        transactionNums: item.transactionNums
      });
    });

      return res;
    }

    let saleItems = [
      {
        month:1,
        date:2,
        transationId: "111",
        salePrice: 200
      },
      {
        month:5,
        date:21,
        transationId: "111",
        salePrice: 1200
      },
      {
        month:1,
        date:12,
        transationId: "111",
        salePrice: 2100
      },
      {
        month:1,
        date:12,
        transationId: "111",
        salePrice: 2100
      },
      {
        month:1,
        date:12,
        transationId: "111",
        salePrice: 2100
      },
      {
        month:11,
        date:22,
        transationId: "111",
        salePrice: 2200
      }
    ];

    let result = sumByQuarter(saleItems);
    console.error(result)
    let result1 = averageByQuarter(saleItems);
    console.error(result1)

	</script>
</body>
</html>
